package com.zhuangwei.spring;

import com.zhuangwei.spring.config.RootConfig;
import com.zhuangwei.spring.config.WebConfig;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * 服务启动类：使用jetty
 *
 * @author 文希
 * @create 2019-05-23 17:55
 **/
public class SpringApplication {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler();

        // 服务器根目录，类似于tomcat服务器部署的项目。完整的访问路径为ip:port/contextPath/realRequestMapping
        // ip:port/项目路径/api请求路径
        handler.setContextPath("/");

        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(WebConfig.class);
        applicationContext.register(RootConfig.class);

        //相当于web.xml中配置的contextLoaderListener
        handler.addEventListener(new ContextLoaderListener(applicationContext));

        //springmvc拦截规则，相当于web.xml中配置的DispatchServlet
        handler.addServlet(new ServletHolder(new DispatcherServlet(applicationContext)), "/");

        server.setHandler(handler);
        server.start();
        server.join();
    }
}
